package com.erp.shop.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.erp.shop.domain.bo.ErpSellerStorePromiseTemplateBo;
import com.erp.shop.domain.vo.ErpSellerStorePromiseTemplateVo;
import com.erp.shop.service.IErpSellerStorePromiseTemplateService;
import com.ruoyi.ae.api.RemoteAeProductService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;

import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 店铺服务模板
 *
 * @author erp
 * @date 2023-03-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/seller/sellerStorePromiseTemplate")
public class ErpSellerStorePromiseTemplateController extends BaseController {

    private final IErpSellerStorePromiseTemplateService iErpSellerStorePromiseTemplateService;
    @Resource
    RemoteAeProductService remoteAeProductService;


    @GetMapping("/selectList")
    public R<Object> selectList(ErpSellerStorePromiseTemplateBo bo) {
        List<ErpSellerStorePromiseTemplateVo> list = iErpSellerStorePromiseTemplateService.queryList(bo);
        List<Map<String, Object>> resList = new ArrayList<>();
        list.forEach(vo -> {
            Map<String, Object> map = new HashMap<>();
            map.put("lable", vo.getTemplateName());
            map.put("value", vo.getTemplateId());
            resList.add(map);
        });

        return R.ok(resList);
    }

    /**
     * 查询店铺服务模板列表
     */
    @SaCheckPermission("seller:sellerStorePromiseTemplate:list")
    @GetMapping("/list")
    public TableDataInfo<ErpSellerStorePromiseTemplateVo> list(ErpSellerStorePromiseTemplateBo bo, PageQuery pageQuery) {
        return iErpSellerStorePromiseTemplateService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取店铺服务模板详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("seller:sellerStorePromiseTemplate:query")
    @GetMapping("/{id}")
    public R<ErpSellerStorePromiseTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpSellerStorePromiseTemplateService.queryById(id));
    }

    /**
     * 新增店铺服务模板
     */
    @SaCheckPermission("seller:sellerStorePromiseTemplate:add")
    @Log(title = "店铺服务模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSellerStorePromiseTemplateBo bo) {
        return toAjax(iErpSellerStorePromiseTemplateService.insertByBo(bo));
    }

    /**
     * 修改店铺服务模板
     */
    @SaCheckPermission("seller:sellerStorePromiseTemplate:edit")
    @Log(title = "店铺服务模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSellerStorePromiseTemplateBo bo) {
        return toAjax(iErpSellerStorePromiseTemplateService.updateByBo(bo));
    }

    /**
     * 和获取店铺服务模版
     * @param storeId
     * @return
     */
    @GetMapping("/queryPromiseTemplateById")
    public R<Object> queryPromiseTemplateById(Long storeId) {
        return remoteAeProductService.queryPromiseTemplateById("-1",storeId);
    }

    /**
     * 删除店铺服务模板
     *
     * @param ids 主键串
     */
    @SaCheckPermission("seller:sellerStorePromiseTemplate:remove")
    @Log(title = "店铺服务模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpSellerStorePromiseTemplateService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
